package com.bc.mcode.collect;

import java.util.*;
import java.util.function.BiConsumer;

public final class Lists {
    private Lists() {
    }

    public static <T> ArrayList<T> newArrayList() {
        return new ArrayList();
    }

    public static <T> ArrayList<T> newArrayList(int capacity) {
        return new ArrayList(capacity);
    }

    @SafeVarargs
    public static <E> ArrayList<E> newArrayList(E... elements) {
        ArrayList<E> list = new ArrayList(elements.length);
        Collections.addAll(list, elements);
        return list;
    }

    public static <E> LinkedList<E> newLinkedList() {
        return new LinkedList();
    }

    @SafeVarargs
    public static <E> LinkedList<E> newLinkedList(E... elements) {
        LinkedList<E> list = new LinkedList();
        Collections.addAll(list, elements);
        return list;
    }

    public static <E> void each(Iterable<? extends E> elements, BiConsumer<Integer, ? super E> action) {
        int index = 0;
        Iterator var3 = elements.iterator();

        while(var3.hasNext()) {
            E element = (E) var3.next();
            action.accept(index++, element);
        }

    }

    public static <T> List<T> retainAll(List<T> list1, List<T> list2, Factor<T> factor) {
        List<T> result = newArrayList();
        List<Object> list = newArrayList(list2.size());
        Iterator var5 = list2.iterator();

        Object item;
        while(var5.hasNext()) {
            item = var5.next();
            list.add(factor.getKey((T) item));
        }

        for(int i = 0; i < list1.size(); ++i) {
            item = list1.get(i);
            Object key = factor.getKey((T) item);
            int index = list.indexOf(key);
            if (index >= 0) {
                result.add(factor.operation((T) item, list2.get(index)));
            }
        }

        return result;
    }

    public interface Factor<T> {
        Object getKey(T var1);

        T operation(T var1, T var2);
    }
}
